
OPT ?= -O2 -DNDEBUG
#OPT ?= -g2
# OPT ?= -O2 -g2 -DNDEBUG

$(shell ./preconfigure .preconfig.mak)
include .preconfig.mak

CXXFLAGS += -I. -I ./include $(OPT)

OBJECTS = $(SOURCES:.cxx=.o)


LIBRARY = libmydb.a

default: all

TEST = segmentparsertest logfiletest iotest readwritetest deletiontest \
concurrentreadwritetest crc32test compacttest logseriaperf ioperftest  \
ratelimitingtest metafiletest

TOOLS = logprint indexarrayprint segmentprint metafileprint

SHARED_MAJOR = 0
SHARED_MINOR = 1 
SHARED1 = libmydb.so
SHARED2 = $(SHARED1).$(SHARED_MAJOR)
SHARED3 = $(SHARED1).$(SHARED_MAJOR).$(SHARED_MINOR)
SHARED = $(SHARED1) $(SHARED2) $(SHARED3)
$(SHARED1): $(SHARED3)
	ln -fs $(SHARED3) lib/$(SHARED1)
$(SHARED2): $(SHARED3)
	ln -fs $(SHARED3) lib/$(SHARED2)


$(SHARED3): $(SOURCES)
	$(CXX) $(LDFLAGS) $(SHARED_LDFLAGS)$(SHARED2) $(CXXFLAGS) $(SHARED_CFLAGS) $(SOURCES) -o lib/$(SHARED3)


all: $(SHARED) $(LIBRARY) $(TEST) $(TOOLS)


clean:
	-rm -rf ./bin ./lib  */*.o .preconfig.mak

$(LIBRARY): $(OBJECTS)
	rm -f lib/$@
	$(AR) -rs lib/$@ $(OBJECTS)

segmentparsertest: test/segmentparsertest.o
	$(CXX) test/segmentparsertest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

logfiletest: test/logfiletest.o $(OBJECTS) 
	$(CXX) test/logfiletest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

iotest: test/iotest.o $(OBJECTS)
	$(CXX) test/iotest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

readwritetest: test/readwritetest.o $(OBJECTS)
	$(CXX) test/readwritetest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

deletiontest: test/deletiontest.o $(OBJECTS)
	$(CXX) test/deletiontest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

concurrentreadwritetest: test/concurrentreadwritetest.o $(OBJECTS)
	$(CXX) test/concurrentreadwritetest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

crc32test: test/crc32test.o $(OBJECTS)
	$(CXX) test/crc32test.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

compacttest: test/compacttest.o $(OBJECTS)
	$(CXX) test/compacttest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

logseriaperf: test/logseriaperf.o $(OBJECTS)
	$(CXX) test/logseriaperf.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

ioperftest: test/ioperftest.o $(OBJECTS)
	$(CXX) test/ioperftest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

ratelimitingtest: test/ratelimitingtest.o $(OBJECTS)
	$(CXX) test/ratelimitingtest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

metafiletest: test/metafiletest.o $(OBJECTS)
	$(CXX) test//metafiletest.o $(OBJECTS) -o bin/test/$@ $(LDFLAGS)

logprint: tools/logprint.o  $(OBJECTS)
	$(CXX) tools/logprint.o $(OBJECTS) -o bin/$@ $(LDFLAGS)

indexarrayprint: tools/indexarrayprint.o $(OBJECTS)
	$(CXX) tools/indexarrayprint.o $(OBJECTS) -o bin/$@ $(LDFLAGS)

segmentprint: tools/segmentprint.o $(OBJECTS)
	$(CXX) tools/segmentprint.o $(OBJECTS) -o bin/$@ $(LDFLAGS)

metafileprint: tools/metafileprint.o $(OBJECTS)
	$(CXX) tools/metafileprint.o $(OBJECTS) -o bin/$@ $(LDFLAGS)

%.o: %.cxx
	$(CXX) $(CXXFLAGS) -c $< -o $@

